package com.shushan.ui;

import android.content.Context;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.shushan.shushanbao.R;


/**
 * @ClassName: PopupDialog
 * @Desciption: 自定义对话框
 * @author: yichaohua
 * @date: 2018-04-13
 */
public class PopupDialog {

    private static final String TAG = "PopupDialog";

    private PopupWindow dialog;
    private Context context;

    protected LayoutInflater inflater;

    private View contentView;
    private View showView;

    private View titleLayout;
    protected TextView mTitle;
    protected TextView mMessage;

    private Button cancelButton;
    private Button okButton;
    private View vButtonSeparate;
    private ImageView iconImg;

    public PopupDialog(Context context) {
        this.context = context;

        inflater = LayoutInflater.from(context);

        registerComponent();
    }

    private void registerComponent() {

        contentView = inflater.inflate(R.layout.custom_dialog, null);

        titleLayout = contentView.findViewById(R.id.dialog_title_layout);

        mTitle = (TextView) contentView.findViewById(R.id.dialog_title);
        showView = contentView.findViewById(R.id.dialog_show_layout);
        showView.setBackgroundResource(R.drawable.round_border_style);
        mMessage = (TextView) contentView.findViewById(R.id.dialog_msg_tv);

        iconImg = (ImageView) contentView.findViewById(R.id.dialog_icon);

        cancelButton = contentView.findViewById(R.id.dialog_cancel_btn);

        vButtonSeparate = contentView.findViewById(R.id.dialog_bottom_divide);

        okButton = contentView.findViewById(R.id.dialog_ok_btn);

    }

    public void dismiss() {

        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    public PopupDialog setGravity(int gravity) {

        this.mMessage.setGravity(gravity);

        return this;
    }

    public PopupDialog setMessage(String message) {

        this.mMessage.setText(message);

        return this;
    }

    public PopupDialog setMessage(CharSequence message) {

        this.mMessage.setText(message);

        return this;
    }

    public PopupDialog setDialogStyle(int resId) {
        this.showView.setBackgroundResource(resId);
        return this;
    }

    public PopupDialog setIcon(int resId) {

        this.iconImg.setImageResource(resId);
        return this;
    }

    public PopupDialog setTitle(String title) {

        this.mTitle.setText(title);
        titleLayout.setVisibility(View.VISIBLE);

        return this;
    }

    public PopupDialog setTitle(CharSequence title) {

        this.mTitle.setText(title);
        titleLayout.setVisibility(View.VISIBLE);

        return this;
    }

    public PopupDialog setTitleBar(boolean show) {

        this.titleLayout.setVisibility(show ? View.VISIBLE : View.GONE);
        return this;
    }

    public PopupDialog getTextSize(float size) {

        float s = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, size, context.getResources()
                .getDisplayMetrics());

        this.mMessage.setTextSize(s);

        return this;
    }

    public PopupDialog setPositiveButton(String label, final DialogOnClickListener listener) {

        okButton.setText(label);



        okButton.setVisibility(View.VISIBLE);

        okButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                close();
                if (listener != null) {
                    listener.onClick(v);
                }
            }
        });

        return this;
    }

    public PopupDialog setNegativeButton(String label, final DialogOnClickListener listener) {

        cancelButton.setText(label);
        cancelButton.setVisibility(View.VISIBLE);

        vButtonSeparate.setVisibility(View.VISIBLE);

        cancelButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                close();
                if (listener != null) {
                    listener.onClick(v);
                }
            }
        });

        return this;
    }

    public void show(View parentView, int width, int height, int gravity, int x, int y) {

        if (dialog == null) {
            dialog = new PopupWindow(contentView, width, height);
        }

        if (!dialog.isShowing() && parentView != null) {
            dialog.showAtLocation(parentView, gravity, x, y);
        }
    }

    public void show(View parentView, int width, int height, int gravity) {

        if (dialog == null) {
            dialog = new PopupWindow(contentView, width, height);
        }

        if (!dialog.isShowing() && parentView != null) {
            dialog.showAtLocation(parentView, gravity, 0, 0);
        }
    }

    public void show(View parentView, int width, int height) {

        if (dialog == null) {
            dialog = new PopupWindow(contentView, width, height, true);
        }

        if (!dialog.isShowing() && parentView != null) {
            dialog.showAtLocation(parentView, Gravity.CENTER, 0, 0);
        }
    }

    public void close() {

        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    public boolean isShowing() {

        if (dialog != null) {
            return dialog.isShowing();
        }

        return false;
    }

    public void initDialog(View parentView, int width, int height) {
        if (dialog == null) {
            dialog = new PopupWindow(contentView, width, height);
        }
    }



    public interface DialogOnClickListener {
        public void onClick(View v);
    }

}
